<?php
/**
 * www.zhuizhan.com.
 * User: junyv
 * Date: 14-11-11 下午2:11
 */
//广告
function getAds($board_id=1,$limit='1'){
    $board = M('adboard')->where(array('id'=>$board_id,'status'=>1))->find();
    if($board){
        $map = array('board_id'=>$board_id,'status'=>1);
        $obj = M('ad')->where($map)->field('type,name,url,img,content,data')->order('ordid desc,id desc');
        if($limit){
            $obj->limit($limit);
        }
        $lists = $obj->select();
        if(is_array($lists) && !empty($lists)){
            foreach($lists as $k=>$v){
                $lists[$k]['width'] = $board['width'];
                $lists[$k]['height'] = $board['height'];
                $lists[$k]['img']    = $v['img'];
                $lists[$k]['data']   = unserialize($v['data']);
            }
        }
        return $lists;
    }else{
        return false;
    }

}
//位置
function catpos($catid){
    static $arr = array();
    static $i = 0;
    $catObj = M('ArticleCate');
    $info = $catObj->where(array('id'=>$catid))->field('id,name,pid,spid')->find();
    $info['islast'] = $i==0 ? 1 : 0;
    if($info['pid']){
        $i++;
        catpos($info['pid']);
    }
    $arr[] = $info;
    return $arr;
}
//计算时间差
function getTime($sTime,$type='mohu'){
    if (!$sTime) return '';
    $cTime = time();
    $dTime = $cTime - $sTime;
    $dDay = intval(date("z", $cTime)) - intval(date("z", $sTime));
    $dYear = intval(date("Y", $cTime)) - intval(date("Y", $sTime));
    if($type == 'normal'){
        if($dTime < 60){
            if($dTime < 10){
                return '刚刚';
            }else{
                return intval(floor($dTime / 10) * 10) . "秒前";
            }
        }elseif($dTime < 3600){
            return intval($dTime / 60) . "分钟前";
        }elseif($dYear == 0 && $dDay == 0){
            return '今天' . date('H:i', $sTime);
        }elseif($dYear == 0){
            return date("m月d日 H:i", $sTime);
        }else{
            return date("Y-m-d H:i", $sTime);
        }
    }elseif($type == 'mohu'){
        if($dTime < 60){
            return $dTime . "秒前";
        }elseif($dTime < 3600){
            return intval($dTime / 60) . "分钟前";
        }elseif($dTime >= 3600 && $dDay == 0){
            return intval($dTime / 3600) . "小时前";
        }elseif($dDay > 0 && $dDay <= 7){
            return intval($dDay) . "天前";
        }elseif($dDay > 7 && $dDay <= 30){
            return intval($dDay / 7) . '周前';
        }elseif($dDay > 30){
            return intval($dDay / 30) . '个月前';
        }
    }elseif($type == 'full'){
        return date("Y-m-d , H:i:s", $sTime);
    }elseif($type == 'ymd'){
        return date("Y-m-d", $sTime);
    }else{
        if($dTime < 60){
            return $dTime . "秒前";
        }elseif($dTime < 3600){
            return intval($dTime / 60) . "分钟前";
        }elseif($dTime >= 3600 && $dDay == 0){
            return intval($dTime / 3600) . "小时前";
        }elseif($dYear == 0){
            return date("Y-m-d H:i:s", $sTime);
        }else{
            return date("Y-m-d H:i:s", $sTime);
        }
    }
}

//获取推荐位
function getPosition($id,$limit=''){
    $where = array('status'=>1);
    if(is_numeric($id)){
        $where['cate_id'] = $id;
    }
    if(is_array($id)){
        $where['cate_id'] = array('in',$id);
    }
    if($limit){
        $position = M('position')->where($where)->order('ordid desc')->limit($limit)->select();
    }else{
        $position = M('position')->where($where)->order('ordid desc')->select();
    }

    $urlArr = array(
        'house'=>'House/detail',
        'used'=>'Used/detail',
        'rental'=>'Rental/detail',
        'estate'=>'Estate/detail',
    );
    $list =  array();
    foreach($position as $v){
        $data = unserialize($v['data']);
        $url = U($urlArr[$v['model']],array('id'=>$v['hid']));
        $list[$v['cate_id']][] = array(
            'url'=>$url,
            'title'=>$v['title'] ? $v['title'] : $data['title'],
            'price'=>$data['price'],
            'img'=>$data['img'],
            'discount'=>$data['discount'] ? $data['discount'] : $data['title'],
            'average_price'=>$data['average_price'],
            'total_price'=>$data['total_price'],
            'address'=>$data['address'],
            'add1'=>$data['add1'],
            'area'=>$data['area'],
            'data'=>$data,
            'type_area'=>$data['room'].'室'.$data['haa'].'厅'.$data['kitchen'].'厨'.$data['toilet'].'卫'.$data['balcony'].'阳台'
        );
    }
    return $list;

}

function get_child_ids($id, $with_self=false,$obj='ArticleCate') {
    $obj = M($obj);
    $spid = $obj->where(array('id'=>$id))->getField('spid');
    $spid = $spid ? $spid .= $id .'|' : $id .'|';
    $id_arr = $obj->field('id')->where(array('spid'=>array('like', $spid.'%')))->select();
    $array = array();
    foreach ($id_arr as $val) {
        $array[] = $val['id'];
    }
    $with_self && $array[] = $id;
    return $array;
}

/**
 * 获取单页文章
 * @param $id
 */
function getPages($id){
        $obj = M('One');
        $info = $obj->where(array('cate_id'=>$id))->find();
    return $info;
}
function jsUnescape($str)
{
    $ret = '';
    $len = strlen($str);
    for ($i = 0; $i < $len; $i++) {
        if ($str[$i] == '%' && $str[$i+1] == 'u') {
            $val = hexdec(substr($str, $i+2, 4));
            if ($val < 0x7f) $ret .= chr($val);
            else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
            else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f));
            $i += 5;
        } else if ($str[$i] == '%') {
            $ret .= urldecode(substr($str, $i, 3));
            $i += 2;
        } else $ret .= $str[$i];
    }
    return $ret;
}
//获取指定时间
function get_date($t='d',$n=0,$a=''){
    $now_time = time();
    $date=date("Y-m-d",$now_time);
    if($t=='d'){
        $firstday = date('Y-m-d 00:00:00',strtotime("$n day"));
        $lastday = date("Y-m-d 23:59:59",strtotime("$n day"));
    }elseif($t=='w'){
        if($n!=0){$date = date('Y-m-d',strtotime("$n week"));}
        $lastday = date("Y-m-d 00:00:00",strtotime("$date Sunday"));
        $firstday = date("Y-m-d 23:59:59",strtotime("$lastday -6 days"));
    }elseif($t=='m'){
        if($n!=0){$date = date('Y-m-d',strtotime("$n months"));}
        $firstday = date("Y-m-01 00:00:00",strtotime($date));
        $lastday = date("Y-m-d 23:59:59",strtotime("$firstday +1 month -1 day"));
        if($n>0){
            $firstday = $a == 'now' ? date("Y-m-01 00:00:00") : $firstday;
        }else{
            $curdate = date("Y-m-d");
            $firstday = $a == 'now' ? date("Y-m-01 00:00:00",strtotime("$firstday +1 month -1 day")) : $firstday;
            $lastday  =  $a == 'now' ? date("Y-m-d 23:59:59",strtotime("$curdate")) : $lastday;
        }

    }else{
        $firstday = date('Y-m-d 00:00:00');
        $lastday = date("Y-m-d 23:59:59");
    }
    return array($firstday,$lastday);
}

function getStoreLeft(){
    $uid = intval($_GET['uid']);
    if($uid){
        $store_user = M('user')->where(array('id'=>$uid))->find();
        $store_user['userInfo'] =  M('user_info')->where(array('user_id'=>$uid))->find();
        $sc = M('store')->where(array('user_id'=>$uid))->count();
        if(!$sc){
            M('store')->add(array('user_id'=>$uid,'name'=>$store_user['username']));
        }
        $store = M('store')->where(array('user_id'=>$uid))->find();
        return array('store_user'=>$store_user,'store'=>$store);
    }
}

function getUserUsedList($user_id,$limit=8){
    $used_list = M('used')->where(array('user_id'=>$user_id))->order('id desc')->limit($limit)->select();
    return $used_list;
}

function getStoreHits($hits){
    $new_hits = str_split(str_pad($hits, 9, "0", STR_PAD_LEFT));
    return $new_hits;
}